﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.EmployeeUI.Profile
{
    public partial class MessageDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadApplication();
        }

        private void LoadApplication()
        {
            try
            {
                int appId = Convert.ToInt32(Request.QueryString["app-Id"]);
                LeaveApplication application = new LeaveApplication();
                ApplicationManager manager = new ApplicationManager();
                application = manager.ReadApplication(appId);
                empCodeLbl.Text = application.Employee.EmpCode.CodeName;
                ViewState["code"] = application.Employee.EmpCode.CodeName;
                empNameLbl.Text = application.Employee.Name;
                empNamelebel.Text = application.Employee.Name;
                empDesignationLbl.Text = application.Employee.Designation.Title;
                empStatusLbl.Text = application.Employee.Status;
                if (application.Status == "Approved")
                {
                    btnApprove.Visible = false;
                }
                empProjectLbl.Text = application.Employee.Project.ProjectName;

                appDate.Text = application.ApplyDate;
                leaveStartlbl.Text = application.StartDate;
                leaveEndLbl.Text = application.EndDate;
                totalDaysLbl.Text = application.TotalDays.ToString();
                ViewState["days"] = application.TotalDays;
                ViewState["type"] = application.LeaveType;
                leaveTypeLbl.Text = application.LeaveType;
                appMessageLbl.Text = application.Message;
                appMessageLbl.Text.Replace("<br/>", char.ConvertFromUtf32(13));
                //  Replace("<br/>", char.ConvertFromUtf32(13));
                appStatusLbl.Text = application.Status;
                appFlagLbl.Text = application.Flag;
                appSubjectLbl.Text = application.Subject;

                LeaveStatus leaveStatus = new LeaveStatus();
                LeaveStatusManager leaveStatusManager = new LeaveStatusManager();
                leaveStatus = leaveStatusManager.GetEmployeeLeaveStatus(application.Employee.EmpCode);
                earnLeaveLbl.Text = leaveStatus.EarnLeave.ToString();
                casualLeaveLbl.Text = leaveStatus.CasualLeave.ToString();
                sickLeaveLbl.Text = leaveStatus.SickLeave.ToString();
                otherLbl.Text = leaveStatus.Others.ToString();
            }
            catch (Exception)
            {
                throw new Exception("");
            }

        }

        protected void ApproveLeave(object o, EventArgs e)
        {
            try
            {
                Code code = new Code();
                code.CodeName = ViewState["code"].ToString();
                LeaveApplication application = new LeaveApplication();

                application.Id = Convert.ToInt32(Request.QueryString["app-Id"]);
                application.TotalDays = Convert.ToInt32(ViewState["days"]);
                application.LeaveType = ViewState["type"].ToString();

                bool remainHoliday = false;
                switch (application.LeaveType)
                {
                    case "Earn Leave": if (Convert.ToInt32(earnLeaveLbl.Text) - application.TotalDays >= 0) { remainHoliday = true; } break;
                    case "Sick Leave": if (Convert.ToInt32(sickLeaveLbl.Text) - application.TotalDays >= 0) { remainHoliday = true; } break;
                    case "Casual Leave": if (Convert.ToInt32(casualLeaveLbl.Text) - application.TotalDays >= 0) { remainHoliday = true; } break;
                    case "Others": if (Convert.ToInt32(otherLbl.Text) - application.TotalDays >= 0) { remainHoliday = true; } break;
                }
                if (remainHoliday)
                {
                    ApplicationManager applicationManager = new ApplicationManager();
                    applicationManager.ApproveLeave(code, application);
                    btnApprove.Visible = false;
                }
                else
                {
                    Response.Write("<script type=text/javascript>");
                    Response.Write("alert('Employer has no leave which he requested !')");
                    Response.Write("</script");
                }

            }
            catch (Exception)
            {
                throw new Exception("");
            }

        }
    }
}